<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<!-- react核心库 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<!-- 支持react操作DOM -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
<!-- 引入Babel，用于将JSX转换为JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<!-- prop-types -->
<script src="https://unpkg.com/prop-types@15.7.2/prop-types.js"></script>

</head>
<body>
<div id="test"></div>
<script type="text/babel"> 
    //生命周期回调函数，生命周期钩子函数 
    class A extends React.Component{
        state = {carName:'奔驰'}
        changeCar = ()=>{
            this.setState({
                carName:'宝马'
            })
        }

        render(){
            return(
            <div>
                <div>我是A组件</div>
                <button onClick={this.changeCar}>换车</button>
                <B carName={this.state.carName}/>         
            </div>
            )
        }
    }


    class B extends React.Component{ 
        //组件将要接收新的props的时候这个函数会被调用
        componentWillReceiveProps(nextProps){
            console.log('B组件的nextProps',nextProps)
        }

        render(){
            return(
            <div>
                我是B组件，我的车是{this.props.carName}
            </div>
            )
        }
    }
    

    ReactDOM.render(<A/>,document.getElementById('test'))
</script>
</body>
</html>